package com.elf.bluetoothtest.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import java.util.UUID;

public class ClassicBluetoothConnection implements IBlueToothConnection {

    private final static String BLUETOOTH_ID = "00001101-0000-1000-8000-00805F9B34FB";

    private final Context mContext;
    private final BluetoothDevice mBluetoothDevice;
    private BluetoothConnectionListener mBluetoothConnectionListener;
    private BluetoothSocket mBluetoothSocket;
    private final BroadcastReceiver mBroadcastReceiver;

    public ClassicBluetoothConnection(Context context, BluetoothDevice device) {
        mContext = context;
        mBluetoothDevice = device;

        mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent == null) {
                    return;
                }
                String action = intent.getAction();
                if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
                    int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, -1);
                    int preState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE, -1);
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    if (BluetoothAdapter.STATE_CONNECTING == state) {

                    } else if (BluetoothAdapter.STATE_CONNECTED == state) {
                        if (mBluetoothConnectionListener != null) {
                            mBluetoothConnectionListener.onConnected(device);
                        }
                    } else if (BluetoothAdapter.STATE_DISCONNECTED == state) {
                        if (mBluetoothConnectionListener != null) {
                            mBluetoothConnectionListener.onDisconnected(device);
                        }
                    } else if (BluetoothAdapter.STATE_DISCONNECTING == state) {

                    }
                }
            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
        context.registerReceiver(mBroadcastReceiver, filter);
    }

    @Override
    public void connect() {
        new Thread() {
            @Override
            public void run() {
                try {
                    mBluetoothSocket = mBluetoothDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(BLUETOOTH_ID));
                    mBluetoothSocket.connect();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    @Override
    public void disconnect() {
        new Thread() {
            @Override
            public void run() {
                try {
                    if (mBluetoothSocket != null) {
                        mBluetoothSocket.close();
                        mBluetoothSocket = null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    @Override
    public void release() {
        disconnect();
        try {
            mContext.unregisterReceiver(mBroadcastReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public BluetoothDevice getDeviceInfo() {
        return mBluetoothDevice;
    }

    @Override
    public void setBluetoothConnectionListener(BluetoothConnectionListener listener) {
        mBluetoothConnectionListener = listener;
    }
}
